#include <iostream>
using namespace std;

struct ListNode {
	int val;
	ListNode *next;
	ListNode(int x) : val(x), next(NULL) {}
};

ListNode *rotateRight(ListNode *head, int k) {
	// Start typing your C/C++ solution below
	// DO NOT write int main() function
	if(k==0 || !head)
		return head;

	ListNode *pFirst = head;
	int index = 1;
	while(index<k && pFirst){
		pFirst = pFirst->next;
		++index;
	}
	// what if k > length?
	if(pFirst){
		return head;
	}
	ListNode *ppSec = NULL;
	ListNode *pSec = head;
	while(pFirst->next){
		pFirst = pFirst->next;
		ppSec= pSec;
		pSec = pSec->next;
	}
	pFirst->next = head;
	ppSec = NULL;
	return pSec;
}

ListNode *rotateRight(ListNode *head, int k) {
	// Start typing your C/C++ solution below
	// DO NOT write int main() function
	if(k==0 || !head)
		return head;
	// find tail
	int length = 1;
	ListNode *pCur = head;
	while(pCur->next){
		pCur = pCur->next;
		++length;
	}
	pCur->next = head;
	// not become a loop, find the new end
	int move = k%length;
	int moveCount = length - move;
	ListNode *pPre = pCur;
	pCur = head;
	while(moveCount>0){
		pPre = pCur;
		pCur = pCur->next;
		--moveCount;
	}
	pPre->next = NULL;
	return pCur;
}


int main(){
	return 0;
}